<?php

/**
 * 夏日葵电商系统
 * http://www.xiarikui.com
 */

function cacTime($num){
    $hour = floor($num/3600);
    $minute = floor(($num-3600*$hour)/60);
    $second = floor((($num-3600*$hour)-60*$minute)%60);
    $str = '';
    if($hour)
        $str .= $hour.'小时';
    if($minute)
        $str .= $minute.'分';
    if($second)
        $str .= $second.'秒';
    return $str;
}

/**
 * 读取/dev/urandom获取随机数
 * @param $len
 * @return mixed|string
 */
function randomFromDev($len) {
    $fp = @fopen('/dev/urandom', 'rb');
    $result = '';
    if ($fp !== FALSE) {
        $result .= @fread($fp, $len);
        @fclose($fp);
    } else {
        trigger_error('Can not open /dev/urandom.');
    }
    // convert from binary to string
    $result = base64_encode($result);
    // remove none url chars
    $result = strtr($result, '+/', '-_');

    return substr($result, 0, $len);
}

function getQuestionResponse($user_id = 0)
{
    if(!$user_id){
        return false;
    }
    $count = M('question')->alias('q')
        ->join('left join __ANSWER__ as a on q.id = a.question_id')
        ->where('q.user_id = '.$user_id.' and q.has_read = 0 and a.id > 0')
        ->count();
    return $count > 0 ? true : false;
}

function getCityName($flag)
{
    $sex = C('SEX');
    return $sex[$flag];
}

function getSexName($flag)
{
    $sex = C('SEX');
    return $sex[$flag];
}

function getCatNameStr($cat_ids = '') {
    $cate =  M('categories')->getField('id,cat_name');

    $temp = explode(',',$cat_ids);
    $arr = array();
    foreach ($cate as $key=>$val){
        if(in_array($key,$temp)){
            $arr[$key] = $val;
        }
    }
    return implode(',',$arr);
}

function getCatName($cat_ids = '') {
    $cate = D('category')->getKVList();

    $temp = explode(',',$cat_ids);
    $arr = array();
    foreach ($cate as $key=>$val){
        if(in_array($key,$temp)){
            $arr[$key] = $val;
        }
    }
    return $arr;
}

function getCatArr($cat_ids = '') {
    $cate = D('category')->getKVList();

    $temp = explode(',',$cat_ids);
    $arr = array();
    foreach ($cate as $key=>$val){
        if(in_array($key,$temp)){
            $arr[] = array('id' => $key , 'cat_name' => $val);
        }
    }
    return $arr;
}

function getQuestionStatus($status) {
    $arr =  C('QUESTION_STATUS');
    return $arr[$status];
}

function getAnswerStatus($status) {
    $arr =  C('ANSWER_STATUS');
    return $arr[$status];
}

function getUserLogType($type) {
    $arr =  C('USER_LOG_TYPE');
    return $arr[$type];
}

function getAccountingLogType($type) {
    $arr =  C('ACCOUNTING_LOG_TYPE');
    return $arr[$type];
}

function getNormalHour(){
    return M('config')
        ->where("inc_type = 'basic' and name = 'normal_hour' ")->getField('value');
}

function getSpeedHour(){
    return M('config')
        ->where("inc_type = 'basic' and name = 'speed_hour' ")->getField('value');
}

function getAccounantRate(){
    return M('config')
        ->where(" inc_type = 'basic' and name = 'accountant_rate' ")->getField('value');
}

function getTestMonth(){
    return M('config')
        ->where(" inc_type = 'basic' and name = 'test_month' ")->getField('value');
}

function getYearMoney(){
    return M('config')
        ->where(" inc_type = 'basic' and name = 'year_money' ")->getField('value');
}

function getPromptDay(){
    return M('config')
        ->where(" inc_type = 'basic' and name = 'prompt_day' ")->getField('value');
}

function update_paylog_from_notify($data){
    if ($data['return_code'] == 'SUCCESS') {
        $shop_sn = $data['out_trade_no'];
        $trade_sn = $data['transaction_id'];
        $money = $data['total_fee'] / 100;
        $attach = json_decode($data['attach'],true);
        //$money = $attach['money'];
        $pay_time = chunk_split($data['time_end'], 2, "|");
        $pay_time = explode('|', $pay_time);
        $pay_time = $pay_time[0] . $pay_time[1] . '-' . $pay_time[2]. '-' . $pay_time[3] . ' ' . $pay_time[4] .':' . $pay_time[5] . ':' . $pay_time[6];
        $rate = 100;//getAccounantRate();
        $save = array(
            'question_id' => $attach['question_id'],
            'user_id' => $attach['user_id'],
            'au_id' => $attach['accountant_id'],
            'type' => $attach['type'],
            'money' => $money,
            'rate' => $rate,
            'status' => 1,
            'trade_sn' => $trade_sn,
            'shop_sn' => $shop_sn,
            'pay_time' => strtotime($pay_time),
            'add_time' => time(),
        );

        if ($attach['from'] == 'wechat') {

            $save['platform'] = 'wechat';

            if ($attach['type'] == 1) {
                $save['type'] = 0; //悬赏待拨入
            }

            M('pay_log')->data($save)->add();

            $field = '';
            if ($attach['type'] == 1) { //悬赏
                jpush($attach['question_id'],$attach['accountant_id']); //悬赏支付后再次推送

                M('question')->where('id = ' . $attach['question_id'] . ' and user_id = ' . $attach['user_id'])->data(array('question_status' => 2 , 'stop_time' => time() + (getSpeedHour() * 60 * 60)))->save(); //以支付时间计算截止时间

                //$field = 'reward_money';
            } elseif ($attach['type'] == 2) {
                $field = 'bonus_money';
            } elseif ($attach['type'] == 3) {
                $field = 'charge_money';
            }

            if ($field) {
                M('accountant_users')->where('au_id = ' . $attach['accountant_id'])->setInc($field, number_format(($money * $rate / 100),2));
            }

        } elseif ($attach['from'] == 'app') {

            $save['method'] = 1;
            $bool = M('accountant_log')->data($save)->add();
            if(!$bool){
                return;
            }
            $find = M('accountant_users')->where('au_id = '.$attach['accountant_id'])->find();
            if(!$find){
                return;
            }
            $time = getPromptDay() * 86400;
            if($find['expired_time'] && $find['expired_time'] + $time > time()){
                $expired_time = strtotime("+1 year", strtotime(date('Y-m-d 23:59:59',$find['expired_time'])));
            }else{
                $expired_time = strtotime("+1 year",strtotime(date('Y-m-d 23:59:59')));
            }
            M('accountant_users')->where('au_id = '.$attach['accountant_id'])->data(array('expired_time' => $expired_time))->save();

        }
    }
}


function jpush($q_id = 0,$accountant_id = 0){
    if(!$q_id){
        return;
    }
    if($accountant_id){
        $au_id = $accountant_id;
    }else{
        $au_id = M('question_answer')->where('question_id = ' . $q_id)->getField('answer_id');
    }

    if(!$au_id){
        return;
    }
    $reg_id = M('accountant_users')->where('au_id = ' . $au_id)->getField('registered_id');
    if ($reg_id) {
        $jpush = new \App\Model\UserModel();
        $jpush->jpush($q_id, $reg_id, '您收到了一条问题，赶快去回复吧！');
    }
}


function makeRequest($url, $params = array(), $expire = 0, $extend = array(), $hostIp = '') {
    if (empty($url)) {
        return array('code' => '100');
    }

    $_curl = curl_init();
    $_header = array(
        'Accept-Language: zh-CN',
        'Connection: Keep-Alive',
        'Cache-Control: no-cache'
    );
    // 方便直接访问要设置host的地址
    if (!empty($hostIp)) {
        $urlInfo = parse_url($url);
        if (empty($urlInfo['host'])) {
            $urlInfo['host'] = substr(DOMAIN, 7, -1);
            $url = "http://{$hostIp}{$url}";
        } else {
            $url = str_replace($urlInfo['host'], $hostIp, $url);
        }
        $_header[] = "Host: {$urlInfo['host']}";
    }

    // 只要第二个参数传了值之后，就是POST的
    if (!empty($params)) {
        curl_setopt($_curl, CURLOPT_POSTFIELDS, http_build_query($params));
        curl_setopt($_curl, CURLOPT_POST, true);
    }

    if (substr($url, 0, 8) == 'https://') {
        curl_setopt($_curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($_curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    }
    curl_setopt($_curl, CURLOPT_URL, $url);
    curl_setopt($_curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($_curl, CURLOPT_USERAGENT, 'API PHP CURL');
    curl_setopt($_curl, CURLOPT_HTTPHEADER, $_header);

    if ($expire > 0) {
        curl_setopt($_curl, CURLOPT_TIMEOUT, $expire); // 处理超时时间
        curl_setopt($_curl, CURLOPT_CONNECTTIMEOUT, $expire); // 建立连接超时时间
    }

    // 额外的配置
    if (!empty($extend)) {
        curl_setopt_array($_curl, $extend);
    }

    $result['result'] = curl_exec($_curl);
    $result['code'] = curl_getinfo($_curl, CURLINFO_HTTP_CODE);
    $result['info'] = curl_getinfo($_curl);
    if ($result['result'] === false) {
        $result['result'] = curl_error($_curl);
        $result['code'] = -curl_errno($_curl);
    }

    curl_close($_curl);
    return $result;
}

/**
 * @param $arr
 * @param $key_name
 * @return array
 * 将数据库中查出的列表以指定的 id 作为数组的键名 
 */
function convert_arr_key($arr, $key_name) {
    $arr2 = array();
    foreach ($arr as $key => $val) {
        $arr2[$val[$key_name]] = $val;
    }
    return $arr2;
}

function encrypt($str) {
    return md5(C("AUTH_CODE") . $str);
}

/**
 * 获取数组中的某一列
 * @param type $arr 数组
 * @param type $key_name  列名
 * @return type  返回那一列的数组
 */
function get_arr_column($arr, $key_name) {
    $arr2 = array();
    foreach ($arr as $key => $val) {
        $arr2[] = $val[$key_name];
    }
    return $arr2;
}

/**
 * 获取url 中的各个参数  类似于 pay_code=alipay&bank_code=ICBC-DEBIT
 * @param type $str
 * @return type
 */
function parse_url_param($str) {
    $data = array();
    $parameter = explode('&', end(explode('?', $str)));
    foreach ($parameter as $val) {
        $tmp = explode('=', $val);
        $data[$tmp[0]] = $tmp[1];
    }
    return $data;
}

/**
 * 二维数组排序
 * @param $arr
 * @param $keys
 * @param string $type
 * @return array
 */
function array_sort($arr, $keys, $type = 'desc') {
    $key_value = $new_array = array();
    foreach ($arr as $k => $v) {
        $key_value[$k] = $v[$keys];
    }
    if ($type == 'asc') {
        asort($key_value);
    } else {
        arsort($key_value);
    }
    reset($key_value);
    foreach ($key_value as $k => $v) {
        $new_array[$k] = $arr[$k];
    }
    return $new_array;
}

/**
 * 多维数组转化为一维数组
 * @param 多维数组
 * @return array 一维数组
 */
function array_multi2single($array) {
    static $result_array = array();
    foreach ($array as $value) {
        if (is_array($value)) {
            array_multi2single($value);
        } else
            $result_array [] = $value;
    }
    return $result_array;
}

/**
 * 友好时间显示
 * @param $time
 * @return bool|string
 */
function friend_date($time) {
    if (!$time)
        return false;
    $fdate = '';
    $d = time() - intval($time);
    $ld = $time - mktime(0, 0, 0, 0, 0, date('Y')); //得出年
    $md = $time - mktime(0, 0, 0, date('m'), 0, date('Y')); //得出月
    $byd = $time - mktime(0, 0, 0, date('m'), date('d') - 2, date('Y')); //前天
    $yd = $time - mktime(0, 0, 0, date('m'), date('d') - 1, date('Y')); //昨天
    $dd = $time - mktime(0, 0, 0, date('m'), date('d'), date('Y')); //今天
    $td = $time - mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')); //明天
    $atd = $time - mktime(0, 0, 0, date('m'), date('d') + 2, date('Y')); //后天
    if ($d == 0) {
        $fdate = '刚刚';
    } else {
        switch ($d) {
            case $d < $atd:
                $fdate = date('Y年m月d日', $time);
                break;
            case $d < $td:
                $fdate = '后天' . date('H:i', $time);
                break;
            case $d < 0:
                $fdate = '明天' . date('H:i', $time);
                break;
            case $d < 60:
                $fdate = $d . '秒前';
                break;
            case $d < 3600:
                $fdate = floor($d / 60) . '分钟前';
                break;
            case $d < $dd:
                $fdate = floor($d / 3600) . '小时前';
                break;
            case $d < $yd:
                $fdate = '昨天' . date('H:i', $time);
                break;
            case $d < $byd:
                $fdate = '前天' . date('H:i', $time);
                break;
            case $d < $md:
                $fdate = date('m月d日 H:i', $time);
                break;
            case $d < $ld:
                $fdate = date('m月d日', $time);
                break;
            default:
                $fdate = date('Y年m月d日', $time);
                break;
        }
    }
    return $fdate;
}

/**
 * 返回状态和信息
 * @param $status
 * @param $info
 * @return array
 */
function arrayRes($status, $info, $url = "") {
    return array("status" => $status, "info" => $info, "url" => $url);
}

/**
 * @param $arr
 * @param $key_name
 * @param $key_name2
 * @return array
 * 将数据库中查出的列表以指定的 id 作为数组的键名 数组指定列为元素 的一个数组
 */
function get_id_val($arr, $key_name, $key_name2) {
    $arr2 = array();
    foreach ($arr as $key => $val) {
        $arr2[$val[$key_name]] = $val[$key_name2];
    }
    return $arr2;
}

/**
 *  自定义函数 判断 用户选择 从下面的列表中选择 可选值列表：不能为空
 * @param type $attr_values
 * @return boolean
 */
function checkAttrValues($attr_values) {
    if ((trim($attr_values) == '') && ($_POST['attr_input_type'] == '1'))
        return false;
    else
        return true;
}

/**
 * 获取用户真实 IP
 */
function getIP() {
    if (getenv("HTTP_CLIENT_IP"))
        $ip = getenv("HTTP_CLIENT_IP");
    else if (getenv("HTTP_X_FORWARDED_FOR"))
        $ip = getenv("HTTP_X_FORWARDED_FOR");
    else if (getenv("REMOTE_ADDR"))
        $ip = getenv("REMOTE_ADDR");
    else
        $ip = "";
    if (preg_match('/^((?:(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(?:25[0-5]|2[0-4]\d|((1\d{2})|([1 -9]?\d))))$/', $ip))
        return $ip;
    else
        return '';
}

// 服务器端IP
function serverIP() {
    return gethostbyname($_SERVER["SERVER_NAME"]);
}

/**
 * 获取 IP  地理位置
 * 淘宝IP接口
 * @Return: array
 */
function getCity($ip = '') {
    if ($ip == '') {
        $url = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json";
        $ip = json_decode(file_get_contents($url), true);
        $data = $ip;
    } else {
        $url = "http://ip.taobao.com/service/getIpInfo.php?ip=" . $ip;
        $ip = json_decode(file_get_contents($url));
        if ((string) $ip->code == '1') {
            return false;
        }
        $data = (array) $ip->data;
    }

    return $data;
}

/**
 * 自定义函数递归的复制带有多级子目录的目录
 * 递归复制文件夹
 * @param type $src 原目录
 * @param type $dst 复制到的目录
 */
//参数说明：            
//自定义函数递归的复制带有多级子目录的目录
function recurse_copy($src, $dst) {
    $now = time();
    $dir = opendir($src);
    @mkdir($dst);
    while (false !== $file = readdir($dir)) {
        if (($file != '.') && ($file != '..')) {
            if (is_dir($src . '/' . $file)) {
                recurse_copy($src . '/' . $file, $dst . '/' . $file);
            } else {
                if (file_exists($dst . DIRECTORY_SEPARATOR . $file)) {
                    if (!is_writeable($dst . DIRECTORY_SEPARATOR . $file)) {
                        exit($dst . DIRECTORY_SEPARATOR . $file . '不可写');
                    }
                    @unlink($dst . DIRECTORY_SEPARATOR . $file);
                }
                if (file_exists($dst . DIRECTORY_SEPARATOR . $file)) {
                    @unlink($dst . DIRECTORY_SEPARATOR . $file);
                }
                $copyrt = copy($src . DIRECTORY_SEPARATOR . $file, $dst . DIRECTORY_SEPARATOR . $file);
                if (!$copyrt) {
                    echo 'copy ' . $dst . DIRECTORY_SEPARATOR . $file . ' failed<br>';
                }
            }
        }
    }
    closedir($dir);
}

// 递归删除文件夹
function delFile($dir, $file_type = '') {
    if (is_dir($dir)) {
        $files = scandir($dir);
        //打开目录 //列出目录中的所有文件并去掉 . 和 ..
        foreach ($files as $filename) {
            if ($filename != '.' && $filename != '..') {
                if (!is_dir($dir . '/' . $filename)) {
                    if (empty($file_type)) {
                        unlink($dir . '/' . $filename);
                    } else {
                        if (is_array($file_type)) {
                            //正则匹配指定文件
                            if (preg_match($file_type[0], $filename)) {
                                unlink($dir . '/' . $filename);
                            }
                        } else {
                            //指定包含某些字符串的文件
                            if (false != stristr($filename, $file_type)) {
                                unlink($dir . '/' . $filename);
                            }
                        }
                    }
                } else {
                    delFile($dir . '/' . $filename);
                    rmdir($dir . '/' . $filename);
                }
            }
        }
    } else {
        if (file_exists($dir))
            unlink($dir);
    }
}

/**
 * 多个数组的笛卡尔积
 *
 * @param unknown_type $data
 */
function combineDika() {
    $data = func_get_args();
    $data = current($data);
    $cnt = count($data);
    $result = array();
    $arr1 = array_shift($data);
    foreach ($arr1 as $key => $item) {
        $result[] = array($item);
    }

    foreach ($data as $key => $item) {
        $result = combineArray($result, $item);
    }
    return $result;
}

/**
 * 两个数组的笛卡尔积
 * @param unknown_type $arr1
 * @param unknown_type $arr2
 */
function combineArray($arr1, $arr2) {
    $result = array();
    foreach ($arr1 as $item1) {
        foreach ($arr2 as $item2) {
            $temp = $item1;
            $temp[] = $item2;
            $result[] = $temp;
        }
    }
    return $result;
}

/**
 * 将二维数组以元素的某个值作为键 并归类数组
 * array( array('name'=>'aa','type'=>'pay'), array('name'=>'cc','type'=>'pay') )
 * array('pay'=>array( array('name'=>'aa','type'=>'pay') , array('name'=>'cc','type'=>'pay') ))
 * @param $arr 数组
 * @param $key 分组值的key
 * @return array
 */
function group_same_key($arr, $key) {
    $new_arr = array();
    foreach ($arr as $k => $v) {
        $new_arr[$v[$key]][] = $v;
    }
    return $new_arr;
}

/**
 * 获取随机字符串
 * @param int $randLength  长度
 * @param int $addtime  是否加入当前时间戳
 * @param int $includenumber   是否包含数字
 * @return string
 */
function get_rand_str($randLength = 6, $addtime = 1, $includenumber = 0) {
    if ($includenumber) {
        $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHJKLMNPQEST123456789';
    } else {
        $chars = 'abcdefghijklmnopqrstuvwxyz';
    }
    $len = strlen($chars);
    $randStr = '';
    for ($i = 0; $i < $randLength; $i++) {
        $randStr.=$chars[rand(0, $len - 1)];
    }
    $tokenvalue = $randStr;
    if ($addtime) {
        $tokenvalue = $randStr . time();
    }
    return $tokenvalue;
}

/**
 * CURL请求
 * @param $url 请求url地址
 * @param $method 请求方法 get post
 * @param null $postfields post数据数组
 * @param array $headers 请求header信息
 * @param bool|false $debug  调试开启 默认false
 * @return mixed
 */
function httpRequest($url, $method, $postfields = null, $headers = array(), $debug = false) {
    $method = strtoupper($method);
    $ci = curl_init();
    /* Curl settings */
    curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
    curl_setopt($ci, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0");
    curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 60); /* 在发起连接前等待的时间，如果设置为0，则无限等待 */
    curl_setopt($ci, CURLOPT_TIMEOUT, 7); /* 设置cURL允许执行的最长秒数 */
    curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
    switch ($method) {
        case "POST":
            curl_setopt($ci, CURLOPT_POST, true);
            if (!empty($postfields)) {
                $tmpdatastr = is_array($postfields) ? http_build_query($postfields) : $postfields;
                curl_setopt($ci, CURLOPT_POSTFIELDS, $tmpdatastr);
            }
            break;
        default:
            curl_setopt($ci, CURLOPT_CUSTOMREQUEST, $method); /* //设置请求方式 */
            break;
    }
    $ssl = preg_match('/^https:\/\//i', $url) ? TRUE : FALSE;
    curl_setopt($ci, CURLOPT_URL, $url);
    if ($ssl) {
        curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
        curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, FALSE); // 不从证书中检查SSL加密算法是否存在
    }
    //curl_setopt($ci, CURLOPT_HEADER, true); /*启用时会将头文件的信息作为数据流输出*/
    curl_setopt($ci, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ci, CURLOPT_MAXREDIRS, 2); /* 指定最多的HTTP重定向的数量，这个选项是和CURLOPT_FOLLOWLOCATION一起使用的 */
    curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ci, CURLINFO_HEADER_OUT, true);
    /* curl_setopt($ci, CURLOPT_COOKIE, $Cookiestr); * *COOKIE带过去** */
    $response = curl_exec($ci);
    $requestinfo = curl_getinfo($ci);
    $http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
    if ($debug) {
        echo "=====post data======\r\n";
        var_dump($postfields);
        echo "=====info===== \r\n";
        print_r($requestinfo);
        echo "=====response=====\r\n";
        print_r($response);
    }
    curl_close($ci);
    return $response;
    //return array($http_code, $response,$requestinfo);
}

/**
 * 过滤数组元素前后空格 (支持多维数组)
 * @param $array 要过滤的数组
 * @return array|string
 */
function trim_array_element($array) {
    if (!is_array($array))
        return trim($array);
    return array_map('trim_array_element', $array);
}

/**
 * 检查手机号码格式
 * @param $mobile 手机号码
 */
function check_mobile($mobile) {
    if (preg_match('/^((1[3,5,8][0-9])|(14[5,7])|(17[0,1,6,7,8]))\d{8}$/', $mobile))
        return true;
    return false;
}

/**
 * 检查邮箱地址格式
 * @param $email 邮箱地址
 */
function check_email($email) {
    if (filter_var($email, FILTER_VALIDATE_EMAIL))
        return true;
    return false;
}

/**
 *   实现中文字串截取无乱码的方法
 */
function getSubstr($string, $start, $length) {
    if (mb_strlen($string, 'utf-8') > $length) {
        $str = mb_substr($string, $start, $length, 'utf-8');
        return $str . '...';
    } else {
        return $string;
    }
}

/**
 * 判断当前访问的用户是  PC端  还是 手机端  返回true 为手机端  false 为PC 端
 * @return boolean
 */

/**
  　　* 是否移动端访问访问
  　　*
  　　* @return bool
  　　 */
function isMobile() {
    // 如果有HTTP_X_WAP_PROFILE则一定是移动设备
    if (isset($_SERVER['HTTP_X_WAP_PROFILE']))
        return true;

    // 如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
    if (isset($_SERVER['HTTP_VIA'])) {
        // 找不到为flase,否则为true
        return stristr($_SERVER['HTTP_VIA'], "wap") ? true : false;
    }
    // 脑残法，判断手机发送的客户端标志,兼容性有待提高
    if (isset($_SERVER['HTTP_USER_AGENT'])) {
        $clientkeywords = array('nokia', 'sony', 'ericsson', 'mot', 'samsung', 'htc', 'sgh', 'lg', 'sharp', 'sie-', 'philips', 'panasonic', 'alcatel', 'lenovo', 'iphone', 'ipod', 'blackberry', 'meizu', 'android', 'netfront', 'symbian', 'ucweb', 'windowsce', 'palm', 'operamini', 'operamobi', 'openwave', 'nexusone', 'cldc', 'midp', 'wap', 'mobile');
        // 从HTTP_USER_AGENT中查找手机浏览器的关键字
        if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT'])))
            return true;
    }
    // 协议法，因为有可能不准确，放到最后判断
    if (isset($_SERVER['HTTP_ACCEPT'])) {
        // 如果只支持wml并且不支持html那一定是移动设备
        // 如果支持wml和html但是wml在html之前则是移动设备
        if ((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html')))) {
            return true;
        }
    }
    return false;
}

//php获取中文字符拼音首字母
function getFirstCharter($str) {
    if (empty($str)) {
        return '';
    }
    $fchar = ord($str{0});
    if ($fchar >= ord('A') && $fchar <= ord('z'))
        return strtoupper($str{0});
    $s1 = iconv('UTF-8', 'gb2312', $str);
    $s2 = iconv('gb2312', 'UTF-8', $s1);
    $s = $s2 == $str ? $s1 : $str;
    $asc = ord($s{0}) * 256 + ord($s{1}) - 65536;
    if ($asc >= -20319 && $asc <= -20284)
        return 'A';
    if ($asc >= -20283 && $asc <= -19776)
        return 'B';
    if ($asc >= -19775 && $asc <= -19219)
        return 'C';
    if ($asc >= -19218 && $asc <= -18711)
        return 'D';
    if ($asc >= -18710 && $asc <= -18527)
        return 'E';
    if ($asc >= -18526 && $asc <= -18240)
        return 'F';
    if ($asc >= -18239 && $asc <= -17923)
        return 'G';
    if ($asc >= -17922 && $asc <= -17418)
        return 'H';
    if ($asc >= -17417 && $asc <= -16475)
        return 'J';
    if ($asc >= -16474 && $asc <= -16213)
        return 'K';
    if ($asc >= -16212 && $asc <= -15641)
        return 'L';
    if ($asc >= -15640 && $asc <= -15166)
        return 'M';
    if ($asc >= -15165 && $asc <= -14923)
        return 'N';
    if ($asc >= -14922 && $asc <= -14915)
        return 'O';
    if ($asc >= -14914 && $asc <= -14631)
        return 'P';
    if ($asc >= -14630 && $asc <= -14150)
        return 'Q';
    if ($asc >= -14149 && $asc <= -14091)
        return 'R';
    if ($asc >= -14090 && $asc <= -13319)
        return 'S';
    if ($asc >= -13318 && $asc <= -12839)
        return 'T';
    if ($asc >= -12838 && $asc <= -12557)
        return 'W';
    if ($asc >= -12556 && $asc <= -11848)
        return 'X';
    if ($asc >= -11847 && $asc <= -11056)
        return 'Y';
    if ($asc >= -11055 && $asc <= -10247)
        return 'Z';
    return null;
}

/**
 * 转换SQL关键字
 *
 * @param unknown_type $string
 * @return unknown
 */
function strip_sql($string) {
    $pattern_arr = array(
        "/\bunion\b/i",
        "/\bselect\b/i",
        "/\bupdate\b/i",
        "/\bdelete\b/i",
        "/\boutfile\b/i",
        "/\bor\b/i",
        "/\bchar\b/i",
        "/\bconcat\b/i",
        "/\btruncate\b/i",
        "/\bdrop\b/i",
        "/\binsert\b/i",
        "/\brevoke\b/i",
        "/\bgrant\b/i",
        "/\breplace\b/i",
        "/\balert\b/i",
        "/\brename\b/i",
        "/\bmaster\b/i",
        "/\bdeclare\b/i",
        "/\bsource\b/i",
        "/\bload\b/i",
        "/\bcall\b/i",
        "/\bexec\b/i",
        "/\bdelimiter\b/i",
    );
    $replace_arr = array(
        'ｕｎｉｏｎ',
        'ｓｅｌｅｃｔ',
        'ｕｐｄａｔｅ',
        'ｄｅｌｅｔｅ',
        'ｏｕｔｆｉｌｅ',
        'ｏｒ',
        'ｃｈａｒ',
        'ｃｏｎｃａｔ',
        'ｔｒｕｎｃａｔｅ',
        'ｄｒｏｐ',
        'ｉｎｓｅｒｔ',
        'ｒｅｖｏｋｅ',
        'ｇｒａｎｔ',
        'ｒｅｐｌａｃｅ',
        'ａｌｅｒｔ',
        'ｒｅｎａｍｅ',
        'ｍａｓｔｅｒ',
        'ｄｅｃｌａｒｅ',
        'ｓｏｕｒｃｅ',
        'ｌｏａｄ',
        'ｃａｌｌ',
        'ｅｘｅｃ',
        'ｄｅｌｉｍｉｔｅｒ',
    );

    return is_array($string) ? array_map('strip_sql', $string) : preg_replace($pattern_arr, $replace_arr, $string);
}

/*
 * 下划线转驼峰
 */
function convertUnderline($str)
{
    $str = preg_replace_callback('/([-_]+([a-z]{1}))/i',function($matches){
        return strtoupper($matches[2]);
    },$str);
    return $str;
}

/*
 * 驼峰转下划线
 */
function humpToLine($str){
    $str = preg_replace_callback('/([A-Z]{1})/',function($matches){
        return '_'.strtolower($matches[0]);
    },$str);
    return $str;
}

function convertHump(array $data){
    $result = [];
    foreach ($data as $key => $item) {
        if (is_array($item) || is_object($item)) {
            $result[humpToLine($key)] = convertHump((array)$item);
        } else {
            $result[humpToLine($key)] = $item;
        }
    }
    return $result;
}
